import axios from 'axios'
import router from '../router/router';
import { MessageBox } from 'element-ui'

//请求拦截器
axios.interceptors.request.use(config => {
  return config
}, error => {
  return Promise.reject(error)
});
//响应拦截器
axios.interceptors.response.use((res) => {
  if(res.data.code === 401) {
    router.replace("/login");
    return;
  }
  if(res.data.code === 500) {
    const errorInfo = res.data.data;
    sessionStorage.setItem('errorInfo', JSON.stringify(errorInfo));
    if (!MessageBox.isShowMessageBox) {
      MessageBox.isShowMessageBox = true;
      MessageBox.confirm('请“查看详情”并将信息反馈给相关的技术人员', '异常错误', {
        confirmButtonText: '查看详情',
        cancelButtonText: '关闭',
        type: 'error',
        closeOnClickModal: false,
        showClose: false,
        center: true,
        customClass: 'common-confirm-box',
        confirmButtonClass: 'common-btn-primary',
        cancelButtonClass: 'common-btn-text'
      }).then(() => {
        MessageBox.isShowMessageBox = false;
        window.open('/#/errorView');
        sessionStorage.removeItem('errorInfo');
      }).catch(() => {
        MessageBox.isShowMessageBox = false;
        sessionStorage.removeItem('errorInfo');
      });
      return;
    }
  }
  return res;
}, (error) => {
  return Promise.reject(error);
});

axios.defaults.baseURL = "/api";
axios.defaults.withCredentials = true;  //允许请求带上跨域cookie

// const ORIGIN = location.origin;
// const PROTOCOL = location.protocol + "//";

// let mapBaseURL = `${PROTOCOL}www.onduty.com`;
// if (ORIGIN.indexOf("ondutyd.com") > -1 || ORIGIN.indexOf("localhost") > -1) {
//   mapBaseURL = `${PROTOCOL}www.ondutyd.com`;
// } else if (ORIGIN.indexOf("yeahfly.cn") > -1) {
//   mapBaseURL = `${PROTOCOL}od.yeahfly.cn`;
// }
export {
  axios/* , mapBaseURL */
}
